<?php
// +----------------------------------------------------------------------
// | Description: 基础类，无需验证权限。
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\index\logic\MemberLogic;
use app\common\validate\MemberValidate;
use app\common\controller\BaseController;
use app\common\model\SystemConfig;
use app\common\event\MemberLogout;
use think\captcha\Captcha;

class Tool extends BaseController
{
    public function login($relogin = false)
    {
        $MemberLogic = MemberLogic::instance();
        $param = input_param([
            'member_name' => '',
            'password' => '',
            'captcha' => '',
        ]);
        //检测
        MemberValidate::instance()->runCheck($param, '', __FUNCTION__);
        //登录
        $userInfo = $MemberLogic->login();
        $userInfo->hidden(['password']);
        $data['userInfo'] = $userInfo->toArray();
        $data['menusList'] = $MemberLogic->getMenuList($userInfo);
        $data['token'] = $MemberLogic->createJwt($userInfo);
        $data['expire'] = config('jwt_config.exp_time');
        return $this->json_responce($data);
    }

    public function logout()
    {
        $param = $this->param;
        event(MemberLogout::class);
        return $this->json_responce('退出成功');
    }

    public function captcha()
    {
        $config = [
            'length' => 4,
        ];
        $id = MemberValidate::instance()->getCaptchaId();
        $captcha = new Captcha($config);
        return $captcha->entry($id);
    }

    public function checkCaptcha()
    {
        $captcha = input('get.captcha');
        $id = MemberValidate::instance()->getCaptchaId();
        return $this->json_responce(captcha_check($captcha, $id));
    }

}
